local jw_qinjia = fk.CreateSkill {
    name = "jw_qinjia",
    anim_type = "drawcard",
    tags = { Skill.Limited }
}

Fk:loadTranslationTable {
    ["jw_qinjia"] = "亲驾",
    [":jw_qinjia"] = "限定技，一名角色的结束阶段结束时，你可以摸Y张牌并执行一个额外的出牌阶段，若如此做，此回合内，你使用牌仅可指定你或该角色为目标(Y为其上轮成为其他角色使用牌指定为目标的次数)",

    ["@qinjia_to-turn"] = "亲驾",

    ["$jw_qinjia1"] = "崇王壤夷，生长尊贵！",
    ["$jw_qinjia2"] = "衣冠华胄，夷盟优冕！",
}
jw_qinjia:addEffect(fk.TargetConfirming, {
    can_refresh = function(self, event, target, player, data)
        return target == player and data.from ~= target and not target:hasSkill(jw_qinjia.name)
    end,
    on_refresh = function(self, event, target, player, data)
        local room = player.room
        room:addPlayerMark(player, "jw_qinjia", 1)
    end,
})
jw_qinjia:addEffect(fk.RoundStart, {
    can_refresh = function(self, event, target, player, data)
        return player:getMark("jw_qinjia") > 0
    end,
    on_refresh = function(self, event, target, player, data)
        local room = player.room
        room:addPlayerMark(player, "jw_qinjia-round", player:getMark("jw_qinjia"))
        room:setPlayerMark(player, "jw_qinjia", 0)
    end,
})

jw_qinjia:addEffect(fk.EventPhaseEnd, {
    can_trigger = function(self, event, target, player, data)
        return player:hasSkill(jw_qinjia.name) and target.phase == target.Finish and target ~= player and
            target:getMark("jw_qinjia-round") > 0 and
            player:usedSkillTimes(jw_qinjia.name, Player.HistoryGame) == 0
    end,
    on_cost = function(self, event, target, player, data)
        return player.room:askToSkillInvoke(player,
            {
                skill_name = jw_qinjia.name,
                prompt = "亲驾：你可以摸" ..
                    target:getMark("jw_qinjia-round") .. "张牌，并执行一个针对" .. target.id .. "的回合"
            })
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        room:addTableMark(player, "@qinjia_to-turn", target.id)
        player:drawCards(target:getMark("jw_qinjia-round"), jw_qinjia.name)
        player:gainAnExtraPhase(Player.Play)
    end,
})

jw_qinjia:addEffect("prohibit", {
    is_prohibited = function(self, from, to, card)
        local mark = from:getTableMark("@qinjia_to-turn")
        return from and #mark ~= 0 and card and not table.contains(mark, to.id)
    end,
})

return jw_qinjia
